<?php

namespace app\admin\controller\god;

use app\common\controller\Backend;

class College extends Backend
{
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('College');
    }


    public function index()
    {
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $result = ['total' => 0, 'rows' => []];
            $total = $this->model->count();
            if (intval($total) > 0) {
                $result['total'] = $total;
                $list = $this->model->order('sort desc')->select();
                $eKeyMap = (new \app\common\model\Enrollment())->getKeyTextAttr();
                $cKeyMap = (new \app\admin\model\Classes())->getKeyTextAttr();
                foreach ($list as $k => $row) {
                    $row['stu_type_text'] = '';
                    if (isset($eKeyMap[$row['stu_type']])) {
                        $row['stu_type_text'] = $eKeyMap[$row['stu_type']];
                    }
                    $row['school_type_text'] = '';
                    if (isset($cKeyMap[$row['school_type']])) {
                        $row['school_type_text'] = $cKeyMap[$row['school_type']];
                    }
                }
                $result['rows'] = $list;
            }

            return json($result);
        }
        return $this->view->fetch();
    }


    private function getEleTypes()
    {
        $stuTypes = model('Enrollment')->column('name', 'id');
        $schoolTypes = model('Classes')->column('name', 'id');
        $exam_area = model('Examarea')->column('name', 'id');
        $this->view->assign('stu_types', $stuTypes);
        $this->view->assign('school_types', $schoolTypes);
        $this->view->assign('exam_area', $exam_area);
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $this->token();
            $args = $this->request->post("row/a");
            if (empty($args['co_name']) || empty($args['co_code'])) {
                $this->error('院校名称与编码都不能为空');
            }
            $ck = $this->model->where('co_code', $args['co_code'])->find();
            if (!empty($ck)) {
                $this->error('院校编码已存在，不能重复添加');
            }
            $data = $this->getArgs($args);
            $save = $this->model->data($data)->save();
            if (empty($save)) {
                $this->error('保存失败');
            }
            $this->success('操作成功');
        }
        $this->getEleTypes();
        return $this->view->fetch();
    }


    public function edit($ids = null)
    {
        $row = $this->model->get(['id' => $ids]);
        if (empty($row)) {
            $this->error('参数异常');
        }
        if ($this->request->isPost()) {
            $this->token();
            $args = $this->request->post("row/a");
            if (empty($args)) {
                $this->error('参数错误');
            }
            $data = $this->getArgs($args);
            $save = $this->model->save($data, ['id' => intval($ids)]);
            if (empty($save)) {
                $this->error('更新失败');
            }
            $this->success('更新成功');
        }
        $this->getEleTypes();
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    private function getArgs($args)
    {
        return [
            'co_name' => $args['co_name'],
            'co_code' => $args['co_code'],
            'co_logo' => $args['co_logo'],
            'introduce_pic' => $args['introduce_pic'],
            'introduce_desc' => $args['introduce_desc'],
            'introduce_detail' => $args['introduce_detail'],
            'stu_type' => $args['stu_type'],
            'school_type' => $args['school_type'],
            'addr_area' => $args['addr_area'],
            'addr_detail' => $args['addr_detail'],
            'sort' => $args['sort'],
            'exam_area' => join(',', $args['exam_area']),
            'stu_stop' => $args['stu_stop'],
            'remark' => $args['remark'],
            'status' => $args['status'],
        ];
    }

}